<template>
  <div :class='className' :style="{height:height,width:width}"></div>
</template>

<script>
  import * as echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'
  export default {
    name: 'SalaryLineChart',
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '360px'
      },
      salaryData: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        chart: null
      };
    },
    watch: {
      salaryData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.setOptions(this.salaryData);
      },
      setOptions(salaryData = []) {
        let legendTitle = []
        salaryData.forEach(e => {
          legendTitle.push(e.year + '');
        });
        this.chart.clear();
        this.chart.setOption({
          xAxis: {
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
            boundaryGap: false,
            axisTick: {
              show: false
            }
          },
          grid: {
            left: 10,
            right: 15,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10],
            textStyle: {
              color: '#fff'
            }
          },
          yAxis: {
		        type: 'log',
            axisTick: {
              show: false
            }
          },
          legend: {
            data: legendTitle
          },
          series: this.setSeries(salaryData)
        }, true);
      },
      setSeries(salaryData) {
        let series = [];
        salaryData.forEach(e => {
          series.push({
            name: e.year,
            smooth: true,
            type: 'line',
            data: e.salary,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          });
        });
        return series;
      }
    }
  }
</script>

<style lang='scss' scoped>

</style>